'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

' Interaction logic for App.xaml
Partial Public Class App
    Inherits System.Windows.Application

    Private Shared _Style As WindowStyle = New WindowStyle With {.AllowTransparency = True, .Opacity = 0.95}

    Public Shared ReadOnly Property DefaultWindowStyle() As WindowStyle
        Get
            Return _Style
        End Get
    End Property

    Private Sub App_Startup(ByVal sender As Object, ByVal e As System.Windows.StartupEventArgs) Handles Me.Startup
        Dim hostWindow As MainWindow
        Dim exceptionWindow As ExceptionWindow
        Dim performanceWindow As PerformanceWindow
        'Dim activitesWindow As ActivitiesWindow
        'Dim avatarSelWindow As TestAvatarSelector

        Try
            AddHandler System.AppDomain.CurrentDomain.UnhandledException, AddressOf OnAppDomainException

            Core.Kernel.Logger.Log("Ux", TraceEventType.Start, "Starting up JCQ.")

            ApplicationService.Initialize(New System.IO.DirectoryInfo(DataStorageDirectoryPath))
            ApplicationService.Current.LoadServiceData()

            hostWindow = New MainWindow

            hostWindow.Content = New SignInPage
            hostWindow.Show()

            exceptionWindow = New ExceptionWindow
            exceptionWindow.Show()

            performanceWindow = New PerformanceWindow
            performanceWindow.Show()

            'activitesWindow = New ActivitiesWindow
            'activitesWindow.Show()

            'avatarSelWindow = New TestAvatarSelector
            'avatarSelWindow.Show()

            MainWindow = hostWindow
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "JCQ Fatal Error", MessageBoxButton.OK, MessageBoxImage.Error)
        End Try
    End Sub

    Private Sub App_Exit(ByVal sender As Object, ByVal e As System.Windows.ExitEventArgs) Handles Me.Exit
        Try
            ApplicationService.Current.SaveServiceData()
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "JCQ Fatal Error", MessageBoxButton.OK, MessageBoxImage.Error)
        End Try
    End Sub

#Region " Unhandled Exceptions "

    Private Sub App_DispatcherUnhandledException(ByVal sender As Object, ByVal e As System.Windows.Threading.DispatcherUnhandledExceptionEventArgs) Handles Me.DispatcherUnhandledException
        MessageBox.Show(e.Exception.ToString, "JCQ Fatal Error", MessageBoxButton.OK, MessageBoxImage.Error)
        e.Handled = True
    End Sub

    Private Sub OnAppDomainException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
        MessageBox.Show(e.ExceptionObject.ToString, "JCQ Fatal Error", MessageBoxButton.OK, MessageBoxImage.Error)
    End Sub

#End Region

    Public Shared Sub ShowStatusWindows()
        Dim transferInfoWindow As TransferWindow

        transferInfoWindow = New TransferWindow

        transferInfoWindow.Show()
    End Sub

#Region " Persistance "
    Public Shared ReadOnly Property DataStorageDirectoryPath() As String
        Get
            Return System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "jcq\")
        End Get
    End Property

    'Private Sub LoadData()
    '    Dim datadirectory As System.IO.DirectoryInfo

    '    Try
    '        datadirectory = New System.IO.DirectoryInfo(DataStorageDirectoryPath)

    '        Debug.WriteLine(String.Format("DataWarehouse Directory: {0}", DataStorageDirectoryPath), "Ux")

    '        Context.GetService(Of IcqInterface.Interfaces.IDataWarehouseService).Load(datadirectory)
    '        _HistoryService.Load()
    '    Catch ex As Exception
    '        Core.Kernel.Exceptions.PublishException(ex)
    '    End Try
    'End Sub

    'Private Sub PersistDataStorage()
    '    Dim datadirectory As System.IO.DirectoryInfo

    '    If Context Is Nothing Then Return

    '    Try
    '        datadirectory = New System.IO.DirectoryInfo(DataStorageDirectoryPath)

    '        Context.GetService(Of IcqInterface.Interfaces.IDataWarehouseService).Save(datadirectory)
    '    Catch ex As Exception
    '        Core.Kernel.Exceptions.PublishException(ex)
    '    End Try
    'End Sub

    'Private Sub SaveHistory()
    '    If _HistoryService Is Nothing Then Return

    '    _HistoryService.Save()
    'End Sub

    'Private Sub SaveIdentities()
    '    _IdentityProvider.Save()
    'End Sub

    'Private Sub SaveData()
    '    PersistDataStorage()
    '    SaveHistory()
    '    SaveIdentities()
    'End Sub
#End Region

End Class

Public Class WindowStyle
    Public Sub New()
        _Opacity = 1
    End Sub

    Private _AllowTransparency As Boolean
    Public Property AllowTransparency() As Boolean
        Get
            Return _AllowTransparency
        End Get
        Set(ByVal value As Boolean)
            _AllowTransparency = value
        End Set
    End Property

    Private _Opacity As Double
    Public Property Opacity() As Double
        Get
            Return _Opacity
        End Get
        Set(ByVal value As Double)
            _Opacity = value
        End Set
    End Property

    Public Sub Attach(ByVal wnd As Window)
        wnd.Opacity = Opacity
        wnd.AllowsTransparency = AllowTransparency
        wnd.WindowStyle = Windows.WindowStyle.None
        wnd.Style = DirectCast(App.Current.FindResource("SimpleWindow"), Style)

        Dim extender = JCsTools.Wpf.Extenders.WindowExtenderProvider.AttachResizeExtender(wnd)

        wnd.Resources("WindowExtender") = extender

        'Style="{StaticResource SimpleWindow}" WindowStyle="None"
    End Sub
End Class